<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
	public function index()
	{				
		$data;
		$user = null;
		
		$this->form_validation->set_rules('headline', 'Headline', 'required|max_length[50]|xss_clean');
		$this->form_validation->set_rules('message', 'Message', 'required|max_length[3000]|xss_clean');
		
		if($this->login_model->IsLoggedIn())
		{
			$user = $this->login_model->GetUser();
			if($user['rank'] > 0)
			{
				if($this->form_validation->run() == FALSE)
				{		
					
				}
				else
				{
					$this->blog_model->post_new_blogpost($user['userId'],$this->input->post("headline"),$this->input->post("message"));
				}
			}
		}
		
		// Pagination
		$startRow = $this->uri->segment(3);
		$perPage = 4;
		
		if(trim($startRow) == '')
		{
			$startRow = 0;
		}
		
		$data['blogposts'] = null;
		
		if($rows = $this->blog_model->get_all_blogposts_rows())
		{				
			$config['base_url'] = base_url() . 'index.php/home/index';
			$config['total_rows'] = $rows;
			$config['per_page'] = 4;
			
			$this->pagination->initialize($config);
			$data['paging'] = $this->pagination->create_links();
			$data['blogposts'] = $this->blog_model->get_blog_limited($startRow, $perPage);
		}
			
		$data['content'] = $this->constantlogistic->HomeConstants(ConstantLogistic::HOME_VIEW_URL,null);
		$data['title'] = $this->constantlogistic->HomeConstants(ConstantLogistic::HOME_VIEW_TITLE,null);
		$data['user'] = $user;
		
		$this->load->view("index", $data);
	}
	
	public function comments()
	{
		$blogId = $this->uri->segment(3);
		
		$this->form_validation->set_rules('message', 'Comment', 'required|max_length[500]|xss_clean');
		
		if($this->login_model->IsLoggedIn())
		{
			$user = $this->login_model->GetUser();
			if($this->form_validation->run() == FALSE)
			{		
				
			}
			else
			{
				$this->blog_model->post_new_blog_comment($blogId,$user['userId'],$this->input->post("message"));
			}	
		}

		if($blogId != null)
		{		
			$data['blogpost'] = $this->blog_model->get_single_blogpost($blogId);
			$data['comments'] = $this->blog_model->get_blogpost_comments($blogId);
		}
		
		$data['title'] = "Comments";
		$data['content'] = "home/comments";
		
		$this->load->view("index", $data);
		
	}
}